package com.love.rongcommon.config.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.love.rongcommon.util.SnowFlake;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;

@Configuration
public class MetaHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        LocalDateTime now = LocalDateTime.now();
        this.setFieldValByName("id", SnowFlake.getInstance().nextId(), metaObject);
        this.setFieldValByName("createTime", now, metaObject);
        this.setFieldValByName("updateTime", now, metaObject);
        if (null == this.getFieldValByName("enable", metaObject)) {
            this.setFieldValByName("enable", Boolean.TRUE, metaObject);
        }
        if (null == this.getFieldValByName("status", metaObject)) {
            this.setFieldValByName("status", 0, metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        if (null == this.getFieldValByName("enable", metaObject)) {
            this.setFieldValByName("enable", Boolean.TRUE, metaObject);
        }
        if (null == this.getFieldValByName("status", metaObject)) {
            this.setFieldValByName("status", 0, metaObject);
        }
    }
}
